Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle case where account entities is empty in after cache access #7329

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from

Conversation

LuccaRebelloToledo
Copy link

Based on the Issue 7324, after implementing the DistributedCachePlugin class, when using the removeAccount method, the credentials were not cleared from my client cache. So, I started investigating and uploaded new personal versions of the library with logs during the entire account removal process and noticed that at the final moment of passing where the cache object was cleared, when trying to recover the KVStore cache, it was empty, that is, it was impossible to identify the partition key, preventing the clearance of the credentials in the client cache. However, I performed a validation where, if the accountEntities were empty, it would search for the partition key from my partition manager, correcting the problem of not clearing the credentials.

Previously, when using the removeAccount method, the accountEntities array was empty during the afterCacheAccess phase, preventing extraction of the partitionKey.

Now, if no accountEntities are available, the partitionKey is fetched from the partitionManager to ensure that the cache can still be updated.

  • Refactored the afterCacheAccess method to check if accountEntities is empty.
  • If accountEntities is empty, fetch partitionKey from partitionManager.

Before the fix:
beforeRemoveAccount

After the fix:
afterRemoveAccount

Test results:
{46EB06E5-254B-47B1-AA48-4EFE055FD220}

@LuccaRebelloToledo
Copy link
Author

@microsoft-github-policy-service agree

Comment on lines -49 to -64
await this.client.set(
partitionKey,
cacheContext.tokenCache.serialize()
);
Copy link
Collaborator

@Robbie-Microsoft Robbie-Microsoft Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of refactoring everything, can you keep this as simple as possible? Lets move this out of the if statement (after it), and add an else for partitionKey = this.partitionManager.getKey()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean something like that?

let partitionKey;

if (accountEntities.length > 0) {
  const accountEntity = accountEntities[0] as AccountEntity;
  
  partitionKey = await this.partitionManager.extractKey(
    accountEntity
  );
} else {
  partitionKey = this.partitionManager.getKey()
}

await this.client.set(
  partitionKey,
  cacheContext.tokenCache.serialize()
);

@Robbie-Microsoft
Copy link
Collaborator

Robbie-Microsoft commented Oct 24, 2024

@LuccaRebelloToledo This fix will require a new unit test in DistributedCachePlugin.spec.ts.

@LuccaRebelloToledo
Copy link
Author

LuccaRebelloToledo commented Oct 25, 2024

@LuccaRebelloToledo This fix will require a new unit test in DistributedCachePlugin.spec.ts.

@Robbie-Microsoft At this point I'm in doubt about how to run the unit test because I found that in TokenCache.spec.ts the data is not fully mocked and actually uses the TokenCache functions. Should I change to use it like in TokenCache.spec.ts or should I continue mocking the functions and results?

…ing cache state consistency post-serialization
Copy link
Author

@LuccaRebelloToledo LuccaRebelloToledo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Result of all tests:
image

Result of tests in DistributedCachePlugin.spec.ts
image

@bgavrilMS
Copy link
Member

Thanks so much for highlighting this issue and proposing a fix @LuccaRebelloToledo

@LuccaRebelloToledo LuccaRebelloToledo requested a review from a team as a code owner November 3, 2024 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
msal-node Related to msal-node package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants